import java.util.concurrent.LinkedBlockingQueue;

//Tabs = 4
//************************************************************
//	COPYRIGHT 2011 Bread Zeppelin - ALL RIGHTS RESERVED
//
//This file is the product of Bread Zeppelin and cannot be 
//reproduced, copied, or used in any shape or form without 
//the express written consent of Bread Zeppelin.
//************************************************************
//
//	$ID: Concurrent_Assg6
//
//	$Date: Feb 13, 2012
//
//	Reason:
//
//*************************************************************

/** $Author: Ben Hare, Zach Huenink, Philipp Wittmaack, Peter Maresca */
/** $Revision: 1 */

//Default Package

/**
 * Acts as a data structure to hold different sized tables. 
 * Handles the trouble of finding a correct sized table.
 */
public class TablePool {

	// Three Table capacities.
	final private int smallTable = 1;
	final private int mediumTable = 4;
	final private int largeTable = 6;

	// Three linked queues to hold the different sized tables.
	private LinkedBlockingQueue<Table> smallTableArray;
	private LinkedBlockingQueue<Table> mediumTableArray;
	private LinkedBlockingQueue<Table> largeTableArray;


	/**
	 * TablePool constructor. Instantiates: small, medium and large, Table Arrays.
	 */
	TablePool() {

		this.smallTableArray = new LinkedBlockingQueue<Table>();
		this.mediumTableArray = new LinkedBlockingQueue<Table>();
		this.largeTableArray = new LinkedBlockingQueue<Table>();

	}

	/**
	 * Returns a table based on the requestedSize.
	 * 
	 * @param requestedSize, the number of people needing to be seated.
	 * @return Returns a table most closely matching the requestedSize if one is available, else null.
	 */
	public Table getTable(int requestedSize){

		if (requestedSize <= smallTable) {

			return smallTableArray.poll();

		} else if (requestedSize <= mediumTable) {

			return mediumTableArray.poll();

		} else if (requestedSize <= largeTable) {

			return largeTableArray.poll();
		} 

		return null;

	}

	public boolean checkAvailability(int requestedSize) {

		if (requestedSize <= smallTable) {

			return !(smallTableArray.peek() == (null) );

		} else if (requestedSize <= mediumTable) {

			return !(mediumTableArray.peek() == (null));

		} else if (requestedSize <= largeTable) {

			return !(largeTableArray.peek() == (null));
		} 

		return false;
	}

	/**
	 * Adds the passed in Table, back into the TablePool.
	 * 
	 * @param t, the table to be placed back into the TablePool. 
	 */
	public void returnTable(Table t) {

		if (t != null) {
			
			t.setClean(true);
			t.setOccupied(false);
			t.setOrder(null);

			if (t.getTableSize() <= smallTable) {

				smallTableArray.add(t);

			} else if (t.getTableSize() <= mediumTable) {

				mediumTableArray.add(t);

			} else {

				largeTableArray.add(t);
			} 
		}
		
		System.out.println(openTableListingString());

	}

	public String openTableListingString() {

		String output = "Table setup:: Ones: "+ smallTableArray.size()
				+" Fours: "+ mediumTableArray.size()
				+" Sixes:"+ largeTableArray.size();
		return output;
	}
	
	public int size() {
		
		return smallTableArray.size()
				+ mediumTableArray.size()
				+ largeTableArray.size();
		
	}

}
